package com.qf.agro.web.controller.device;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.agro.common.sys.ResultInfo;
import com.qf.agro.pojo.device.SmartDevice;
import com.qf.agro.service.device.SmartDeviceService;
import com.qf.agro.vo.OptionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @company：千锋教育
 * @author：zhy
 * @date：2023/7/2
 */
@RestController
@RequestMapping("/device/device")
public class SmartDeviceController {

    @Autowired
    private SmartDeviceService smartDeviceService;



    @GetMapping(value = "/selectByfacilitiesId",name = "根据农业设施id查询智能设备")
    public ResultInfo selectByFacilitiesId(String facilitiesId) {
        //1.调用业务层
        List<SmartDevice> smartDevices = smartDeviceService.selectByFacilitiesId(facilitiesId);
        return ResultInfo.success(smartDevices);
    }

    /**
     * 分页查询智能设备
     *
     * @param pageNum
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public ResultInfo findPage(@RequestParam(defaultValue = "1", value = "page") Integer pageNum, @RequestParam(defaultValue = "5") Integer pageSize, String name, String facilitiesId) {
        //1.调用业务层查询
        Page page = smartDeviceService.findPage(pageNum, pageSize, name, facilitiesId);
        //2.返回
        return ResultInfo.success(page);
    }


    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @GetMapping
    public ResultInfo findById(Long id) {
        //1.调用业务层查询
        SmartDevice smartDevice = smartDeviceService.findById(id);
        //2.返回
        return ResultInfo.success(smartDevice);
    }


    /**
     * 保存智能设备分类
     *
     * @param smartDevice
     * @return
     */
    @PostMapping
    public ResultInfo save(@RequestBody SmartDevice smartDevice) {
        //1.调用业务层保存
        smartDeviceService.save(smartDevice);
        //2.返回
        return ResultInfo.success(null);
    }

    /**
     * 更新智能设备分类
     *
     * @param smartDevice
     * @return
     */
    @PutMapping
    public ResultInfo update(@RequestBody SmartDevice smartDevice) {
        //1.调用业务层更新
        smartDeviceService.update(smartDevice);
        //2.返回
        return ResultInfo.success(null);
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @DeleteMapping
    public ResultInfo delete(Long id) {
        //1.调用业务层删除
        smartDeviceService.delete(id);
        //2.返回
        return ResultInfo.success(null);
    }




    @GetMapping("selectDeviceOptions")
    public ResultInfo selectDeviceOptions(String facilitiesIdSelect){
        List<OptionVo>voList = smartDeviceService.selectOptions(facilitiesIdSelect);
        return ResultInfo.success(voList);
    }


    @PutMapping(value = "/updateStateById")
    public ResultInfo updateStateById(String  deviceId) {
        int i = smartDeviceService.updateStateById(Long.valueOf(deviceId));
        if (i == 0) {
            return ResultInfo.error("操作失败");
        } else {
            return ResultInfo.success(null);
        }
    }
}
